package com.leisai.manager.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.leisai.manager.common.utils.DateUtils;
import com.leisai.manager.dto.IndustryDTO;
import com.leisai.manager.pojo.BaseAdminUser;
import com.leisai.manager.pojo.Industry;
import com.leisai.manager.response.PageDataResult;
import com.leisai.manager.service.IndustryService;
import com.leisai.manager.service.ReportService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: jim
 * @Date: 2021/11/30/14:59
 * @Description:
 */
@Controller
@RequestMapping("industry")
public class IndustryController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IndustryService industryService;

    @Autowired
    private ReportService reportService;

    /**
     *
     * 功能描述: 跳到行业管理
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021-11-23
     */
    @RequestMapping("industryManage")
    public String IndustryManage() {
        logger.info("进入行业管理");
        return "industry/industryManage";
    }

    /**
     *
     * 功能描述: 跳到行业管理
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021-11-23
     */
    @RequestMapping("addIndustryPage")
    public String addIndustryPage(Industry industry , Model model) {
        logger.info("进入行业编辑");
        model.addAttribute("IndustryId",industry.getIndustryId());
        return "industry/addIndustry";
    }


    /**
     *
     * 功能描述: 获取行业列表
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/11/30 10:30
     */
    @RequestMapping(value = "/industryList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult industryList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize, IndustryDTO industrySearch){
        logger.info("获取行业列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 20;
            }
            // 获取服务类目列表
            pdr = industryService.getIndustryList(pageNum ,pageSize,industrySearch);
            logger.info("行业列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("行业列表查询异常！", e);
        }
        return pdr;
    }

    /**
     *
     * 功能描述:设置行业[新增或更新]
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/12/01 9:42
     */
    @PostMapping("setIndustry")
    @ResponseBody
    public Map<String,Object> setIndustry(Industry industry) {
        logger.info("设置行业[新增或更新]！Industry:" + industry);
        Map<String,Object> data = new HashMap();
        if(industry.getIndustryId() == null || industry.getIndustryId().equals("") ){
            //获取当前登陆的用户信息
            BaseAdminUser user = (BaseAdminUser) SecurityUtils.getSubject().getPrincipal();
            industry.setCreateTime(DateUtils.getCurrentDateToDate());
            industry.setUserId(user.getId());
            industry.setStatus(1);
            //设置pids属性
            Industry  pIndustry = industryService.getIndustryById(industry.getPid().intValue());
            if (pIndustry.getPids()==null){
                industry.setPids("["+pIndustry.getIndustryId()+"]");
            }else {
                industry.setPids(pIndustry.getPids() + ",[" + pIndustry.getIndustryId() + "]");
            }
            industry.setUpdateTime(DateUtils.getCurrentDateToDate());
            //新增行业
            data = industryService.addIndustry(industry);
        }else{
            //修改行业
            Industry industryById = industryService.getIndustryById(industry.getIndustryId());
            industry.setUpdateTime(DateUtils.getCurrentDateToDate());
            industry.setCreateTime(industryById.getCreateTime());
            industry.setStatus(industryById.getStatus());
            industry.setPids(industryById.getPids());
            industry.setPid(industryById.getPid());
            industry.setUserId(industryById.getUserId());
            data = industryService.updateIndustry(industry);
        }
        return data;
    }

    /**
     *
     * 功能描述: 删除行业
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2018/11/30 12:02
     */
    @PostMapping("del")
    @ResponseBody
    public Map<String, Object> del(Industry industry) {
        logger.info("删除行业！id:" + industry.getIndustryId());
        Map<String, Object> data = new HashMap<>();

        data = industryService.del(industry);
        return data;
    }
    /**
     *
     * 功能描述: 首页树图数据
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/11/30 10:30
     */
    @RequestMapping(value = "/getIndustryTree", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getReportDate() {
        logger.info("获取行业树状图");

        JSONObject json = reportService.getIndustrytTree();
        return json;

    }
    /**
     *
     * 功能描述: 跳到行业树图首页
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021-11-23
     */
    @RequestMapping("home")
    public String home() {
        logger.info("进入首页");
        return "home";
    }
}
